how to check if a number is a perfect square python

40

import math

# Taking the input from user
number = int(input("Enter the Number"))

root = math.sqrt(number)
if int(root + 0.5) ** 2 == number:
    print(number, "is a perfect square")
else:
    print(number, "is not a perfect square")
import math

def is_perfect_square(number: int) -> bool:
    """
    Returns True if the provided number is a
    perfect square (and False otherwise).
    """
    return math.isqrt(number) ** 2 == number

Comments

Submit
0 Comments